package com.example.library.adminfragment;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.library.DatabaseHelper;
import com.example.library.R;

public class AdminReportFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_admin_report, container, false);
        fillReport(view);
        return view;
    }

    private void fillReport(View view) {
        TextView tvTotalUsers = view.findViewById(R.id.tv_total_users);
        TextView tvBlacklistCount = view.findViewById(R.id.tv_blacklist_count);
        TextView tvTotalReservations = view.findViewById(R.id.tv_total_reservations);
        TextView tvSeatCount = view.findViewById(R.id.tv_seat_count);

        DatabaseHelper dbHelper = new DatabaseHelper(requireContext());
        SQLiteDatabase db = null;
        Cursor cursor = null;
        
        try {
            db = dbHelper.getReadableDatabase();

            // 总人数
            int totalUsers = 0;
            cursor = db.rawQuery("SELECT COUNT(*) FROM user", null);
            if (cursor != null && cursor.moveToFirst()) {
                totalUsers = cursor.getInt(0);
            }
            if (cursor != null) {
                cursor.close();
                cursor = null;
            }

            // 黑名单人数（status=2）
            int blacklistCount = 0;
            cursor = db.rawQuery("SELECT COUNT(*) FROM user WHERE status=2", null);
            if (cursor != null && cursor.moveToFirst()) {
                blacklistCount = cursor.getInt(0);
            }
            if (cursor != null) {
                cursor.close();
                cursor = null;
            }

            // 预约人数（reservation表总数）
            int totalReservations = 0;
            cursor = db.rawQuery("SELECT COUNT(*) FROM reservation", null);
            if (cursor != null && cursor.moveToFirst()) {
                totalReservations = cursor.getInt(0);
            }
            if (cursor != null) {
                cursor.close();
            }

            // 座位数（大厅24+单人12+小组6）
            int seatCount = 24 + 12 + 6;

            tvTotalUsers.setText(String.valueOf(totalUsers));
            tvBlacklistCount.setText(String.valueOf(blacklistCount));
            tvTotalReservations.setText(String.valueOf(totalReservations));
            tvSeatCount.setText(String.valueOf(seatCount));
            
        } catch (Exception e) {
            Toast.makeText(requireContext(), "加载数据失败，请稍后重试", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
            
            // 设置默认值
            tvTotalUsers.setText("0");
            tvBlacklistCount.setText("0");
            tvTotalReservations.setText("0");
            tvSeatCount.setText("42");
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
    }
}